{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "separated-enclosure", "metadata": {}, "outputs": [], "source": [ "# This short notebook will show how to take symbolic derivatives in Python.\n", "# We will require the 'SymPy' module.\n", "import sympy as sym" ] }, { "cell_type": "code", "execution_count": 2, "id": "precious-accent", "metadata": {}, "outputs": [], "source": [ "# First, let's define a symbol x.\n", "x = sym.Symbol('x')" ] }, { "cell_type": "code", "execution_count": 4, "id": "filled-understanding", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 2 x^{2} + 3$" ], "text/plain": [ "2*x**2 + 3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Then we can define f in terms of x.\n", "f = 2*x**2+3\n", "f" ] }, { "cell_type": "code", "execution_count": 5, "id": "manual-exemption", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2(2)^2 + 3 = 11\n" ] } ], "source": [ "# To evaluate f at a particular value of x, we can use 'subs()'.\n", "z = f.subs(x, 2)\n", "print('2(2)^2 + 3 =', z)" ] }, { "cell_type": "code", "execution_count": 6, "id": "stopped-minneapolis", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 4 x$" ], "text/plain": [ "4*x" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To take the derivative of f with respect to x, we use 'diff()'.\n", "dfdx = f.diff(x)\n", "dfdx" ] }, { "cell_type": "code", "execution_count": 7, "id": "durable-valley", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "df/dx at x = 3 is 12\n" ] } ], "source": [ "# Of course, we can now evaluate the derivative at a particular value of x.\n", "print('df/dx at x = 3 is', dfdx.subs(x, 3))" ] }, { "cell_type": "code", "execution_count": 8, "id": "committed-conditions", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 2 \\sin^{y^{2}}{\\left(x \\right)}$" ], "text/plain": [ "2*sin(x)**(y**2)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can make our functions more complicated. Here's a function of two variables.\n", "y = sym.Symbol('y')\n", "g = 2*sym.sin(x)**y**2\n", "g" ] }, { "cell_type": "code", "execution_count": 11, "id": "finished-credit", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1.00273593133124$" ], "text/plain": [ "1.00273593133124" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's how you can use 'subs()' to subsitute values for multiple variables.\n", "z = g.subs({x:1, y:2})\n", "sym.N(z)" ] }, { "cell_type": "code", "execution_count": 12, "id": "domestic-triple", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 y^{2} \\sin^{y^{2}}{\\left(x \\right)} \\cos{\\left(x \\right)}}{\\sin{\\left(x \\right)}}$" ], "text/plain": [ "2*y**2*sin(x)**(y**2)*cos(x)/sin(x)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's the x-derivative of g...\n", "g.diff(x)" ] }, { "cell_type": "code", "execution_count": 13, "id": "focal-investor", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 4 y \\log{\\left(\\sin{\\left(x \\right)} \\right)} \\sin^{y^{2}}{\\left(x \\right)}$" ], "text/plain": [ "4*y*log(sin(x))*sin(x)**(y**2)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# and here's the y-derivative.\n", "g.diff(y)" ] }, { "cell_type": "code", "execution_count": 14, "id": "located-voltage", "metadata": {}, "outputs": [], "source": [ "# Alternatively, you can define Python functions.\n", "def fcn(x):\n", " return 2*x**2 + 3" ] }, { "cell_type": "code", "execution_count": 15, "id": "hydraulic-indicator", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2(2)^2 + 3 = 11\n" ] } ], "source": [ "# One advantage of this method is that it is easy to now evaluate the value \n", "# of fcn at any x.\n", "z = fcn(2)\n", "print('2(2)^2 + 3 =', z)" ] }, { "cell_type": "code", "execution_count": 17, "id": "sacred-recipe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dg/dx = 4*x\n" ] } ], "source": [ "# The derivatives of these functions are calculated in the way.\n", "dgdx = fcn(x).diff(x)\n", "print('dg/dx =', dgdx)" ] }, { "cell_type": "code", "execution_count": 18, "id": "photographic-label", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dg/dx = 4*x\n" ] } ], "source": [ "# Note that there is another way to call the 'diff()' function.\n", "dgdx = sym.diff(fcn(x), x)\n", "print('dg/dx =', dgdx)" ] }, { "cell_type": "code", "execution_count": 19, "id": "north-animal", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1.00273593133124$" ], "text/plain": [ "1.00273593133124" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Of course, you can have functions of multiple variables.\n", "def gcn(x, y):\n", " return 2*sym.sin(x)**y**2\n", "sym.N(gcn(1, 2))" ] }, { "cell_type": "code", "execution_count": 20, "id": "acknowledged-object", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2*y**2*sin(x)**(y**2)*cos(x)/sin(x), 4*y*log(sin(x))*sin(x)**(y**2))" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can evaluate the partial derivatives in the way that you'd expect:\n", "gcn(x, y).diff(x), gcn(x, y).diff(y)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }